export const emailRule = (value: string) => {
  const pattern = /^[^\s@]+@[^\s@]+\.[^\s@]+$/
  return pattern.test(value) || '请输入有效的邮箱地址'
}

export const phoneRule = (value: string) => {
  const pattern = /^1[3-9]\d{9}$/
  return pattern.test(value) || '请输入有效的手机号码'
}

export const passwordRule = (value: string) => {
  if (value.length < 6) {
    return '密码长度至少6位'
  }
  if (value.length > 20) {
    return '密码长度不能超过20位'
  }
  return true
}

export const usernameRule = (value: string) => {
  if (value.length < 3) {
    return '用户名长度至少3位'
  }
  if (value.length > 20) {
    return '用户名长度不能超过20位'
  }
  const pattern = /^[a-zA-Z0-9_]+$/
  return pattern.test(value) || '用户名只能包含字母、数字和下划线'
}

export const requiredRule = (value: any) => {
  return !!value || '此项为必填项'
}

export const confirmPasswordRule = (password: string) => {
  return (value: string) => {
    return value === password || '两次密码输入不一致'
  }
}